package server;

/* HashStructure
 * ---------------------------------------------------
 * Written by
 * Rebecca Van Bussel 6271316
 * October 6, 2013
 */

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;

//class using this structure must handle synchronization.
public class HashStructure {

	private ArrayList<LinkedList<Record> > table = new ArrayList<LinkedList<Record> >();
	private int cRecordCount=0;
	private int mRecordCount=0;
	{{
		for (int i=0;i<26;++i)
			table.add(new LinkedList<Record>());
	}}

	public int getcRecordCount() {
		return cRecordCount;
	}

	public int getmRecordCount() {
		return mRecordCount;
	}
	
	public Record getRecord(String id){
		Record record;
		for (int i=0;i<table.size();++i) {
			for (int j=0;j<table.get(i).size();++j) {
				ListIterator<Record> temp = table.get(i).listIterator();
				while (temp.hasNext()) {
					record=temp.next();
					if (record.getId().equals(id)) {
						return record;
					}
				}
			}
		}
		return null;
	}

	public boolean addRecord (Record record) {
		int hashNum=getHashNum(record);
		if (!table.get(hashNum).contains(record)) {
			table.get(hashNum).add(record);
			if (record.getId().charAt(0)=='M')
				++mRecordCount;
			else 
				++cRecordCount;
			return true;
		}
		return false;
	}

	public boolean editRecord (String lname, String id, String status) {
		int hashNum=getHashNum(lname);
		Record temp=new Record(id);
		if (table.get(hashNum).contains(temp)) {
			table.get(hashNum).get(table.get(hashNum).indexOf(temp)).setStatus(status);
			return true;
		}
		return false; //record does not exist
	}

	public boolean containsRecord (Record record) {
		int hashNum=getHashNum(record);
		return table.get(hashNum).contains(record);
	}

	/*public Record findRecord (String id) {
		Record record;
		for (int i=0;i<table.size();++i) {
			for (int j=0;j<table.get(i).size();++j) {
				ListIterator<Record> temp = table.get(i).listIterator();
				while (temp.hasNext()) {
					record=temp.next();
					if (record.getId().equals(id))
						return record;
				}
			}
		}
		return null;
	}*/

	public Record removeRecord (String id) {
		Record record;
		for (int i=0;i<table.size();++i) {
			for (int j=0;j<table.get(i).size();++j) {
				ListIterator<Record> temp = table.get(i).listIterator();
				while (temp.hasNext()) {
					record=temp.next();
					if (record.getId().equals(id)) {
						temp.remove();
						if (record.getId().charAt(0)=='M')
							--mRecordCount;
						else 
							--cRecordCount;
						return record;
					}
				}
			}
		}
		return null;
	}

	private int getHashNum (Record record) {
		return record.getLname().toUpperCase().charAt(0)-65;
	}

	private int getHashNum (String lname) {
		return lname.toUpperCase().charAt(0)-65;
	}

	public String toString() {
		String returnVal="";		
		for (int i=0;i<table.size();++i)
			if (!table.get(i).isEmpty())
				returnVal+="\n"+(char)(i+65)+"] "+table.get(i).toString();	
		return returnVal;
	}
	
	public String getOfficerRecords(String officerId) {
		String returnVal="";		
		for (int i=0;i<table.size();++i)
			if (!table.get(i).isEmpty()) {
				boolean printLetter=true;	
				for (int j=0;j<table.get(i).size();++j) {
					if (table.get(i).get(j).getOfficerId().equals(officerId))
					{
						if (printLetter) {
							returnVal+="\n"+(char)(i+65)+"] ";
							printLetter=false;
						}
						returnVal+=table.get(i).get(j).toString();
					}
				}
			}
		return returnVal;
	}

	public void incMCount() {
		mRecordCount++;	
	}

	public void incCCount() {
		cRecordCount++;		
	}

}
